<template>
  <div class="category">
    <h1>{{id?'编辑':"新增"}}管理员</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="管理员名称:">
        <el-input v-model="form.username" autocomplete="on"></el-input>
      </el-form-item>
      <el-form-item label="管理员名称:">
        <el-input v-model="form.password" autocomplete="on" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import http from "../axios/index.js";
export default {
  props: {
    id: String,
  },
  data() {
    return {
      form: { username: null, password: null },
      parents: [],
    };
  },
  created() {
    this.id && this.fetch();
  },
  methods: {
    // 获取
    async fetch() {
      const res = await this.$http.get(`/rest/adminUsers/${this.id}`);
      this.form = res.data;
    },
    async save() {
      let res;
      if (this.id) {
        res = await this.$http.put(`/rest/adminUsers/${this.id}`, this.form);
      } else {
        res = await this.$http.post("/rest/adminUsers", this.form);
      }
      const { data } = res;
      if (data.code == 200) {
        this.$message.success(data.msg);
        this.$router.push("/adminUsers/list");
      }
    },
  },
};
</script>

<style></style>
